#!/bin/bash

Hadoop_daemon="/opt/hadoop/bin/hadoop-daemon.sh"

start_hadoop() {
if [ -x "$Hadoop_daemon" ]; then
    if [ "$USER" = "waue" ]; then
        $Hadoop_daemon start namenode
        $Hadoop_daemon start jobtracker
        $Hadoop_daemon start tasktracker
        $Hadoop_daemon start datanode
    else
        su waue -c "$Hadoop_daemon start namenode"
        su waue -c "$Hadoop_daemon start jobtracker"
        su waue -c "$Hadoop_daemon start tasktracker"
        su waue -c "$Hadoop_daemon start datanode"
    fi
else
    echo "[Error: cann't execute $Hadoop_daemon"
fi
}

stop_hadoop() {
if [ -x "$Hadoop_daemon" ]; then
    if [ "$USER" = "waue" ]; then
        $Hadoop_daemon stop datanode
        $Hadoop_daemon stop tasktracker
        $Hadoop_daemon stop jobtracker
        $Hadoop_daemon stop namenode
    else
        su waue -c "$Hadoop_daemon stop datanode"
        su waue -c "$Hadoop_daemon stop tasktracker"
        su waue -c "$Hadoop_daemon stop jobtracker"
        su waue -c "$Hadoop_daemon stop namenode"
    fi
else
    echo "[Error: cann't execute $Hadoop_daemon"
fi
}

case $1 in
    start)
        start_hadoop
        echo "[start hadoop completion !]"
    ;;
    stop)
        stop_hadoop
        echo "[stop hadoop completion !]"
    ;;
    restart)
        stop_hadoop
        sleep 3
        start_hadoop
    ;;
    *)
        echo "Usage: $0 {start/stop/restart}"
        exit 1
    ;;
esac

exit 0

